<?php

/**
 *    普通订单类型
 *
 *    @author    Garbin
 *    @usage    none
 */
class NormalOrder extends BaseOrder {

    var $_name = 'normal';

    /**
     *    查看订单
     *
     *    @author    Garbin
     *    @param     int $order_id
     *    @param     array $order_info
     *    @return    array
     */
    function get_order_detail($order_id, $order_info) {
        if (!$order_id) {
            return array();
        }

        /* 获取商品列表 */
        $data['goods_list'] = $this->_get_goods_list($order_id);

        /* 配关信息 */
        $data['order_extm'] = $this->_get_order_extm($order_id);

        /* 支付方式信息 */
        if ($order_info['payment_id']) {
            $payment_model = & m('payment');
            $payment_info = $payment_model->get("payment_id={$order_info['payment_id']}");
            $data['payment_info'] = $payment_info;
        }

        /* 订单操作日志 */
        $data['order_logs'] = $this->_get_order_logs($order_id);

        return array('data' => $data);
    }

    /* 显示订单表单 */

    function get_order_form($store_id) { // delivery
        $data = array();

        /* 配送方式 */
        $data['shipping_methods']   = $this->_get_shipping_methods($store_id);
        if (empty($data['shipping_methods']))
        {
            $this->_error('no_shipping_methods');

            return false;
        }
        foreach ($data['shipping_methods'] as  $key => $shipping)
        {
            $data['shipping_options'][$shipping['shipping_id']] = $shipping['shipping_name'];
            $data['shipping_methods'][$key]['cod_regions_price']=unserialize($shipping['cod_regions_price']);
        }
        $data['shippings']          = ecm_json_encode($data['shipping_methods']);
        
        return $data;
    }

    /**
     *    提交生成订单，外部告诉我要下的单的商品类型及用户填写的表单数据以及商品数据，我生成好订单后返回订单ID
     *
     *    @author    Garbin
     *    @param     array $data
     *    @return    int
     */
    function submit_order($data) {
        /* 释放goods_info和post两个变量 */
        extract($data);
        /* 处理订单基本信息 */
        $base_info = $this->_handle_order_info($goods_info, $post);
        if (!$base_info) {
            /* 基本信息验证不通过 */

            return 0;
        }

        /* 处理订单收货人信息 */
        $consignee_info = $this->_handle_consignee_info($goods_info, $post);
        if (!$consignee_info) {
            /* 收货人信息验证不通过 */
            return 0;
        }

        /* 至此说明订单的信息都是可靠的，可以开始入库了 */

        /* 插入订单基本信息 */
        //订单总实际总金额，可能还会在此减去折扣等费用
        
        //优惠后的商品总额
        $discount=$base_info['goods_amount'] - $base_info['discount'];
        if($discount > 0){
            $base_info['order_amount'] = $base_info['goods_amount'] + $consignee_info['shipping_fee'] - $base_info['discount'];
        }else{ /* 如果优惠金额大于商品总额 */
            
            $base_info['order_amount'] =$consignee_info['shipping_fee'];
            $base_info['discount'] = $base_info['goods_amount'];
        }


        $order_model = & m('order');
        $order_id = $order_model->add($base_info);

        if (!$order_id) {
            /* 插入基本信息失败 */
            $this->_error('create_order_failed');

            return 0;
        }

        /* 插入收货人信息 */
        $consignee_info['order_id'] = $order_id;
        $order_extm_model = & m('orderextm');
        $order_extm_model->add($consignee_info);

        /* 插入商品信息 */
        $goods_items = array();
        foreach ($goods_info['items'] as $key => $value) {
            $goods_items[] = array(
                'order_id' => $order_id,
                'goods_id' => $value['goods_id'],
                'goods_name' => $value['goods_name'],
                'spec_id' => $value['spec_id'],
                'specification' => $value['specification'],
                'price' => $value['price'],
//                lichen 2019/4/18 start
                'price_amount' => $value['price_amount'],
                'tax_price' => $value['tax_price'],
                'tax_price_amount' => $value['tax_price_amount'],
                'price_total' => $value['price_total'],
//                lichen 2019/4/18 end
                'quantity' => $value['quantity'],
                'goods_image' => $value['goods_image'],
                'goods_old_price'=>$value['goods_old_price'],
                'goods_type'=>$value['goods_type'],
                'promotions_id'=>$value['promotions_id'],
            );
        }
        $order_goods_model = & m('ordergoods');
        $order_goods_model->add(addslashes_deep($goods_items)); //防止二次注入
        return $order_id;
    }

}

?>